<template>
  <div>
    <template v-if="ishead && (itemdata.sUITypeExp == 'long' || itemdata.sUITypeExp == 'big')">
      <template v-if="itemdata.lMustInput === '1' && itemdata.lCanInput === '0'">
        <van-field type="textarea" rows="1" autosize :id="itemdata.sField" required disabled v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" />
      </template>
      <template v-else-if="itemdata.lMustInput === '1'">
        <van-field placeholder="请输入" type="textarea" rows="1" autosize :id="itemdata.sField" required v-model="itemdata.value" :label="itemdata.sTitle" @input="checkData(itemdata)" @blur="checkData(itemdata)" :error-message="itemdata.errormsg" />
      </template>
      <template v-else-if="itemdata.lCanInput === '0'">
        <van-field type="textarea" rows="1" autosize :id="itemdata.sField" disabled v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" />
      </template>
      <template v-else>
        <van-field placeholder="请输入" type="textarea" rows="1" autosize :id="itemdata.sField" v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" />
      </template>
    </template>
    <template v-else-if="ishead && itemdata.sFormat == 'star'">
      <template v-if="itemdata.lMustInput === '1' && itemdata.lCanInput === '0'" required>
        <van-cell :title="itemdata.sTitle">
          <template #default>
            <van-rate v-model="itemdata.value" :size="25" color="#ffd21e" void-icon="star" void-color="#eee" readonly />
          </template>
        </van-cell>
      </template>
      <template v-else-if="itemdata.lMustInput === '1'" required placeholder="请输入">
        <van-cell :title="itemdata.sTitle">
          <template #default>
            <van-rate v-model="itemdata.value" :size="25" color="#ffd21e" void-icon="star" void-color="#eee" />
          </template>
        </van-cell>
      </template>
      <template v-else-if="itemdata.lCanInput === '0'">
        <van-cell :title="itemdata.sTitle">
          <template #default>
            <van-rate v-model="itemdata.value" :size="25" color="#ffd21e" void-icon="star" void-color="#eee" readonly />
          </template>
        </van-cell>
      </template>
      <template v-else placeholder="请输入">
        <van-cell :title="itemdata.sTitle">
          <template #default>
            <van-rate v-model="itemdata.value" :size="25" color="#ffd21e" void-icon="star" void-color="#eee" />
          </template>
        </van-cell>
      </template>
    </template>
    <template v-else-if="ishead && (itemdata.sUITypeExp == 'normal' && itemdata.sFormat == 'yearmonth;')">
      <template v-if="itemdata.lMustInput === '1' && itemdata.lCanInput === '0'">
        <van-field :id="itemdata.sField" required disabled v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" />
      </template>
      <template v-else-if="itemdata.lMustInput === '1'">
        <van-field placeholder="请输入" :id="itemdata.sField" required v-model="itemdata.value" :label="itemdata.sTitle" @click="openYearMonth(itemdata)" :error-message="itemdata.errormsg" />
      </template>
      <template v-else-if="itemdata.lCanInput === '0'">
        <van-field :id="itemdata.sField" disabled v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" />
      </template>
      <template v-else>
        <van-field placeholder="请输入" :id="itemdata.sField" v-model="itemdata.value" :label="itemdata.sTitle" @click="openYearMonth(itemdata)" :error-message="itemdata.errormsg" />
      </template>
    </template>
    <template v-else-if="ishead && (itemdata.idObject == 'account' || itemdata.idObject == 'account1' || itemdata.idObject == 'account2') && itemdata.sField == 'sname'">
      <van-field autocomplete="off" :id="itemdata.sField" required v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" @input="solrMatch(itemdata)" placeholder="请输入">
        <van-button slot="button" size="mini" v-if="solrshow" @click="closesolr"><van-icon name="arrow-up" /></van-button>
      </van-field>
      <div id="solrListDiv" :style="solrListDivStyle">
        <van-list :immediate-check="false" v-model="solrLoading" :finished="solrFinished" finished-text="加载完">
          <van-cell v-for="solrItem in solrList" :key="solrItem.id" :title="solrItem.sname" @click="choseSolr(solrItem)" />
        </van-list>
      </div>
    </template>
    <template v-else-if="ishead && (itemdata.sUITypeExp == 'normal' && itemdata.sFormat.indexOf('refer:') >= 0)">
      <template v-if="itemdata.lMustInput === '1' && itemdata.lCanInput === '0'">
        <van-field :id="itemdata.sField" required disabled v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" />
      </template>
      <template v-else-if="itemdata.lMustInput === '1'">
        <van-field placeholder="请输入" :id="itemdata.sField" required readonly v-model="itemdata.value" :label="itemdata.sTitle" @click="openRefer(itemdata)" :error-message="itemdata.errormsg" />
      </template>
      <template v-else-if="itemdata.lCanInput === '0'">
        <van-field :id="itemdata.sField" disabled v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" />
      </template>
      <template v-else>
        <van-field placeholder="请输入" :id="itemdata.sField" v-model="itemdata.value" readonly :label="itemdata.sTitle" @click="openRefer(itemdata)" :error-message="itemdata.errormsg" />
      </template>
    </template>
    <template v-else-if="ishead">
      <template v-if="itemdata.lMustInput === '1' && itemdata.lCanInput === '0'">
        <van-field :id="itemdata.sField" required disabled v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" />
      </template>
      <template v-else-if="itemdata.lMustInput === '1'">
        <van-field placeholder="请输入" :id="itemdata.sField" required clearable v-model="itemdata.value" :label="itemdata.sTitle" @input="checkData(itemdata)" @blur="checkData(itemdata)" :error-message="itemdata.errormsg" />
      </template>
      <template v-else-if="itemdata.lCanInput === '0'">
        <van-field :id="itemdata.sField" disabled v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" />
      </template>
      <template v-else>
        <van-field placeholder="请输入" :id="itemdata.sField" clearable v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" />
      </template>
    </template>
    <template v-else-if="itemdata.sUITypeExp == 'long' || itemdata.sUITypeExp == 'big'">
      <template v-if="itemdata.lMustInput === '1' && itemdata.lCanInput === '0'">
        <van-field type="textarea" rows="1" autosize :id="index + '_' + itemdata.sField" required disabled v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" />
      </template>
      <template v-else-if="itemdata.lMustInput === '1'">
        <van-field placeholder="请输入" type="textarea" rows="1" autosize :id="index + '_' + itemdata.sField" required v-model="itemdata.value" :label="itemdata.sTitle" @input="checkData(itemdata)" @blur="checkData(itemdata)" :error-message="itemdata.errormsg" />
      </template>
      <template v-else-if="itemdata.lCanInput === '0'">
        <van-field type="textarea" rows="1" autosize :id="index + '_' + itemdata.sField" disabled v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" />
      </template>
      <template v-else>
        <van-field placeholder="请输入" type="textarea" rows="1" autosize :id="index + '_' + itemdata.sField" v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" />
      </template>
    </template>
    <template v-else-if="itemdata.sUITypeExp == 'normal' && itemdata.sFormat == 'yearmonth;'">
      <template v-if="itemdata.lMustInput === '1' && itemdata.lCanInput === '0'">
        <van-field :id="index + '_' + itemdata.sField" required disabled v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" />
      </template>
      <template v-else-if="itemdata.lMustInput === '1'">
        <van-field placeholder="请输入" :id="index + '_' + itemdata.sField" required v-model="itemdata.value" :label="itemdata.sTitle" @click="openYearMonth(itemdata)" :error-message="itemdata.errormsg" />
      </template>
      <template v-else-if="itemdata.lCanInput === '0'">
        <van-field :id="index + '_' + itemdata.sField" disabled v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" />
      </template>
      <template v-else>
        <van-field placeholder="请输入" :id="index + '_' + itemdata.sField" v-model="itemdata.value" :label="itemdata.sTitle" @click="openYearMonth(itemdata)" :error-message="itemdata.errormsg" />
      </template>
    </template>
    <template v-else>
      <template v-if="itemdata.lMustInput === '1' && itemdata.lCanInput === '0'">
        <van-field :id="index + '_' + itemdata.sField" required disabled v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" />
      </template>
      <template v-else-if="itemdata.lMustInput === '1'">
        <van-field placeholder="请输入" :id="index + '_' + itemdata.sField" required clearable v-model="itemdata.value" :label="itemdata.sTitle" @input="checkData(itemdata)" @blur="checkData(itemdata)" :error-message="itemdata.errormsg" @change="pageCal(indexName, itemdata.sField, index)" />
      </template>
      <template v-else-if="itemdata.lCanInput === '0'">
        <van-field :id="index + '_' + itemdata.sField" disabled v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" />
      </template>
      <template v-else>
        <van-field placeholder="请输入" :id="index + '_' + itemdata.sField" clearable v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" @change="pageCal(indexName, itemdata.sField, index)" />
      </template>
    </template>
    <van-action-sheet v-model="enumshow" :actions="actions" @select="selectedEnum" />
    <van-overlay :show="overlayShow" :z-index="1000">
      <div style="display: flex;align-items: center;justify-content: center;height: 100%;">
        <van-loading type="spinner" color="#1989fa">请稍候...</van-loading>
      </div>
    </van-overlay>
    <van-popup v-model="yearmonthshow" round position="bottom">
      <van-datetime-picker v-model="currentYearMonth" type="year-month" @confirm="selectedYearMonth" />
    </van-popup>
  </div>
</template>
<script>
import { Toast, DropdownMenu, DropdownItem, Popup, Icon } from 'vant'
import Vue from 'vue'
import { keepNDecimalFull } from '@/assets/js/tool.js'
import qs from 'qs'
Vue.use(Toast).use(DropdownMenu).use(DropdownItem).use(Popup).use(Icon)
export default {
  data: function () {
    return {
      enumshow: false,
      overlayShow: false,
      yearmonthshow: false,
      actions: [],
      isDisable: false,
      currentYearMonth: new Date(),
      clickmodel: {},
      solrval: '',
      solrList: [],
      solrLoading: true,
      solrFinished: false,
      solrListDivStyle: {
        display: 'none',
        position: 'absolute',
        'z-index': 999,
        background: 'white',
        border: '1px solid',
        right: '0px'
      },
      solrshow: false
    }
  },
  props: {
    idobject: {
      type: String,
      required: false
    },
    jsondata: {
      type: Object,
      required: false
    },
    indexName: {
      type: String,
      required: false
    },
    itemdata: {
      type: Object,
      required: false
    },
    ishead: {
      type: Boolean,
      default: false
    },
    index: {
      type: Number,
      required: false
    }
  },
  methods: {
    selectedEnum (item) {
      this.clickmodel.valuehidden = item.id
      this.clickmodel.value = item.name
      this.clickmodel.errormsg = ''
      this.enumshow = false
      this.boolUpdate(this.clickmodel)
    },
    checkData (datamodel) {
      if (datamodel.value === '') {
        datamodel.errormsg = '不能为空'
      } else {
        datamodel.errormsg = ''
      }
    },
    openYearMonth (datamodel) {
      if (datamodel.value != null && datamodel.value !== undefined && datamodel.value !== '') {
        datamodel.value = datamodel.value.substring(0, 7)
        this.currentYearMonth = new Date(datamodel.value)
      }
      this.clickmodel = datamodel
      this.yearmonthshow = true
    },
    solrMatch (datamodel) {
      this.solrshow = true
      if (datamodel.value === '') {
        datamodel.errormsg = ''
      } else {
        let lData = { sname: datamodel.value }
        this.axios({
          method: 'post',
          url: '/api/' + this.idobject + '.querysolr.do',
          data: qs.stringify(lData)
        }).then(res => {
          this.isDisable = false
          this.overlayShow = false
          if (res.data.flag === '0') {
            let solrJsondata = res.data.jsondata
            for (let i = 0; i < solrJsondata.length; i++) {
              this.solrList[i] = solrJsondata[i]
            }
            this.solrLoading = true
            this.solrFinished = true
            this.showsolr()
          } else {
            Toast(res.data.des)
            if (res.data.des && res.data.des.indexOf('登录过期') >= 0) {
              this.loginout()
            }
          }
          //console.log(res)
        }).catch(res => {
          this.isDisable = false
          this.overlayShow = false
          //console.log(res)
        })
      }
    },
    choseSolr (datamodel) {
      for (let groupindex in this.jsondata.head) {
        let itemgroup = this.jsondata.head[groupindex].groupvalue
        for (let itemindex in itemgroup) {
          let itemdata = itemgroup[itemindex]
          if (itemdata.sField === 'sname') {
            itemdata.value = datamodel.sname
          } else if (itemdata.sField === 'sheng') {
            itemdata.valuehidden = datamodel.idsheng
            itemdata.value = datamodel.idsheng_ref
          } else if (itemdata.sField === 'shi') {
            itemdata.valuehidden = datamodel.idshi
            itemdata.value = datamodel.idshi_ref
          } else if (itemdata.sField === 'emterritory') {
            itemdata.valuehidden = datamodel.idquxian
            itemdata.value = datamodel.idquxian_ref
          } else if (itemdata.sField === 'saddress') {
            itemdata.value = datamodel.saddress
          } else if (itemdata.sField === 'swebsite') {
            itemdata.value = datamodel.swebsite
          } else if (itemdata.sField === 'stel') {
            itemdata.value = datamodel.stel
          } else if (itemdata.sField === 'spostcode') {
            itemdata.value = datamodel.spostcode
          } else if (itemdata.sField === 'jyzt') {
            itemdata.value = datamodel.regstatus
          } else if (itemdata.sField === 'hospgrade') {
            itemdata.valuehidden = datamodel.hospgrade
            itemdata.value = datamodel.hospgrade_enum
          } else if (itemdata.sField === 'idparent') {
            itemdata.value = datamodel.sjkh
          } else if (itemdata.sField === 'yytype') {
            itemdata.value = datamodel.yylx
          } else if (itemdata.sField === 'khsx') {
            itemdata.valuehidden = datamodel.khsx
            itemdata.value = datamodel.khsx_enum
          } else if (itemdata.sField === 'idclientall') {
            itemdata.valuehidden = datamodel.id
            itemdata.value = datamodel.sname
          }
        }
      }
      this.closesolr()
    },
    showsolr () {
      this.solrshow = true
      this.solrListDivStyle['display'] = 'block'
    },
    closesolr () {
      this.solrshow = false
      this.solrListDivStyle['display'] = 'none'
    },
    openRefer (datamodel) {
      this.actions = []
      this.clickmodel = datamodel
      let referVal = datamodel.sFormat.substring(6, datamodel.sFormat.length - 1)
      let arrRefer = referVal.split(',')
      for (let i = 0; i < arrRefer.length; i++) {
        this.actions.push({ id: arrRefer[i], name: arrRefer[i] })
      }
      this.enumshow = true
    },
    selectedYearMonth () {
      //console.log(this.currentYearMonth)
      let year = this.currentYearMonth.getFullYear()
      let month = this.currentYearMonth.getMonth() + 1
      if (month < 10) {
        month = '0' + month
      }
      let datestring = year + '-' + month
      this.clickmodel.value = datestring
      this.clickmodel.errormsg = ''
      this.yearmonthshow = false
    },
    pageCal (indexName, sField, index) {
      // 需货计划明细 跟 销售计划（年）明细 的字段一样
      if (indexName !== '销售订单明细' && indexName !== '需货计划明细' && indexName !== '销售计划（年）明细') {
        return
      }
      let item = this.jsondata.bodydata[indexName][index]

      let quantity = 0.0
      let xyj = 0.0
      let price = 0.0
      let money = 0.0
      let zr = 0.0
      let kl = 0.0
      let js = 0.0
      let bz = 0.0
      let number = 0.0 // 需货计划  上报数量
      let jianshu = 0.0 // 需货计划 件数
      let hsl = 0.0 // 需货计划 换算率
      let quantityindex = ''
      let xyjindex = ''
      let priceindex = ''
      let moneyindex = ''
      let zrindex = ''
      let klindex = ''
      let jsindex = ''
      let jianshuindex = '' // 需货计划 件数
      let numberindex = '' // 需货计划 上报数量
      let jianshuNumberBool = '' // 需货计划 如果填了件数,数量就不要反过来把件数改了..判断标识

      for (let itemindex in item) {
        let itemdata = item[itemindex]
        if (itemdata.sField === 'quantity') {
          quantity = itemdata.value
          quantityindex = itemindex
        } else if (itemdata.sField === 'xyj') {
          xyj = itemdata.value
          xyjindex = itemindex
        } else if (itemdata.sField === 'price') {
          price = itemdata.value
          priceindex = itemindex
        } else if (itemdata.sField === 'money') {
          money = itemdata.value
          moneyindex = itemindex
        } else if (itemdata.sField === 'zr') {
          zr = itemdata.value
          zrindex = itemindex
        } else if (itemdata.sField === 'kl') {
          kl = itemdata.value
          klindex = itemindex
        } else if (itemdata.sField === 'js') {
          js = itemdata.value
          jsindex = itemindex
        } else if (itemdata.sField === 'bz') {
          bz = itemdata.value
        } else if (itemdata.sField === 'jianshu') {
          jianshu = itemdata.value
          jianshuindex = itemindex
        } else if (itemdata.sField === 'number') {
          number = itemdata.value
          numberindex = itemindex
        } else if (itemdata.sField === 'hsl') {
          hsl = itemdata.value
        }
      }
      if (!Number(xyj)) {
        xyj = price
      }
      if (sField === 'quantity') {
        money = quantity * price
        zr = quantity * (xyj - (price))
        js = quantity / bz
      } else if (sField === 'xyj') {
        price = xyj * kl / 100
        money = quantity * price
        zr = quantity * (xyj - (price))
      } else if (sField === 'price') {
        money = quantity * price
        if (!Number(xyj)) {
          return
        }
        kl = price / xyj * 100
        zr = quantity * (xyj - (price))
      } else if (sField === 'money') {
        if (!Number(quantity)) {
          return
        }
        price = money / quantity
        if (!Number(xyj)) {
          return
        }
        kl = price / xyj * 100
        zr = quantity * xyj - (money)
      } else if (sField === 'zr') {
        money = quantity * xyj - (zr)
        if (!Number(quantity)) {
          return
        }
        price = money / quantity
        if (!Number(xyj)) {
          return
        }
        kl = price / xyj * 100
      } else if (sField === 'kl') {
        if (!Number(kl)) {
          return
        }
        price = xyj * kl / 100
        money = quantity * price
        zr = quantity * (xyj - (price))
      } else if (sField === 'js') {
        if (!Number(js)) {
          return
        }
        quantity = js * bz
        money = quantity * price
      } else if (sField === 'jianshu') {
        if (!Number(jianshu)) {
          return
        }
        if (!jianshuNumberBool) {
          number = jianshu * hsl
          jianshuNumberBool = true
        }
      } else if (sField === 'number') {
        if (!Number(number)) {
          return
        }
        if (!jianshuNumberBool) {
          jianshu = number / hsl
          jianshuNumberBool = true
        }
      }
      if (!quantity) {
        quantity = 0.0
      }
      if (!xyj) {
        xyj = 0.0
      }
      if (!price) {
        price = 0.0
      }
      if (!money) {
        money = 0.0
      }
      if (!zr) {
        zr = 0.0
      }
      if (!kl) {
        kl = 0.0
      }

      if (!jianshu) {
        jianshu = 0.0
      }

      if (!number) {
        number = 0.0
      }

      if (quantityindex) {
        item[quantityindex].value = keepNDecimalFull(quantity, 2)
      }
      if (xyjindex) {
        item[xyjindex].value = keepNDecimalFull(xyj, 6)
      }
      if (priceindex) {
        item[priceindex].value = keepNDecimalFull(price, 6)
      }
      if (moneyindex) {
        item[moneyindex].value = keepNDecimalFull(money, 6)
      }
      if (zrindex) {
        item[zrindex].value = keepNDecimalFull(zr, 2)
      }
      if (klindex) {
        item[klindex].value = keepNDecimalFull(kl, 2)
      }
      if (jsindex) {
        item[jsindex].value = keepNDecimalFull(js, 6)
      }

      if (jianshuindex) {
        item[jianshuindex].value = keepNDecimalFull(jianshu, 6)
      }
      if (numberindex) {
        item[numberindex].value = keepNDecimalFull(number, 2)
      }
    }
  }
}
</script>
